package com.iwayin.core.bo.user;

import java.util.ArrayList;
import java.util.List;

import com.iwayin.core.bo.BaseBO;
import com.iwayin.core.dao.follow.FollowDAO;
import com.iwayin.core.dao.user.UserDAO;
import com.iwayin.core.dataobject.FollowDO;
import com.iwayin.core.dataobject.UserDO;
import com.iwayin.core.utils.PasswordUtil;
import com.iwayin.core.utils.exception.BOException;
import com.iwayin.core.vo.SearchVO;

public class UserBOImpl extends BaseBO implements UserBO {

	private UserDAO userDAO;
	private FollowDAO followDAO;

	@Override
	public Long addUser(UserDO user) {
		// 检查用户名是否被用
		if (getUserByLoginName(user.getLoginName()) != null){
			throw new BOException("邮箱已被使用");
		}
		// 邮箱是否被用
		if (getUserByNickName(user.getNickname()) != null){
			throw new BOException("昵称很热，换一个吧");
		}
		try {
			// 加密密码
			String randomKey = PasswordUtil.getRandomOperPassword();
			user.setPwdKey(randomKey);
			user.setLoginPwd(PasswordUtil.getEncyptedOperPassword(user.getLoginPwd(), randomKey));
			user.setTrueName(user.getNickname());
			return this.userDAO.addUser(user);
		} catch (Exception e) {
			loger.error("创建用户异常" + e);
			throw new BOException("系统异常:稍后重试");
		}
	}
	
	@Override
	public UserDO updateActivateUser(UserDO user) {
		user = getUserById(user.getId());
		if(user!=null && user.getStatus().equals(UserDO.USER_NOT_ACTIVATE_STATUS)){
			user.setStatus(UserDO.USER_ACTIVE_STATUS);
			userDAO.updateUserStatus(user);
		}
		return user;
	}

	@Override
	public UserDO getUserById(Long userId) {
		return this.userDAO.getUserById(userId);
	}

	@Override
	public UserDO getUserByLoginName(String loginName) {
		return userDAO.getUserByLoginName(loginName);
	}
	
	@Override
	public UserDO getUserByNickName(String nickname) {
		return userDAO.getUserByNickName(nickname);
	}

	@Override
	public Integer updatePassword(UserDO user) {
		return this.userDAO.updatePassword(user);
	}

	@Override
	public List<UserDO> listUser(UserDO query) {
		return userDAO.listUser(query);
	}

	@Override
	public UserDO getUserByEmail(String email) {
		return userDAO.getUserByEmail(email);
	}

	@Override
	public void updateUserInfo(UserDO user) {
		UserDO queryUser ;
		UserDO currentUser =this.userDAO.getUserById(user.getId());
		if(!currentUser.getNickname().equals(user.getNickname())){
			queryUser=this.userDAO.getUserByNickName(user.getNickname());
			if(queryUser!=null){
				throw new BOException("昵称太热，被别人抢走啦，换一个吧");
			}
		}
		
		if(!currentUser.getEmail().equals(user.getEmail())){
			queryUser=this.userDAO.getUserByEmail(user.getEmail());
			if(queryUser!=null){
				throw new BOException("邮箱已被使用");
			}
		}
		this.userDAO.updateUserInfo(user);
	}
	
	@Override
	public void updateUserForOpen(UserDO user) {
		// 加密密码
		String randomKey = PasswordUtil.getRandomOperPassword();
		user.setPwdKey(randomKey);
		user.setLoginPwd(PasswordUtil.getEncyptedOperPassword(user.getLoginPwd(), randomKey));
		this.userDAO.updateUserInfo(user);
	}

	@Override
	public List<SearchVO> searchUser(SearchVO query) {
		List<SearchVO> searchList = this.userDAO.searchUser(query);
		if (searchList != null && searchList.size() > 0) {
			for (SearchVO user : searchList) {
				FollowDO friend = this.followDAO.getFollower(
						query.getLoginId(), user.getId());
				if (friend == null) {
					user.setFollow(false);//未关注
				} else {
					user.setFollow(true);//已经关注
				}
			}
		}
		return searchList;
	}
	
	@Override
	public List<UserDO> recommendFriends(Long loginId, int userSize) {
		userSize = userSize == 0 ? 3 : userSize;
		List<Long> systemUserIds = this.userDAO.findSystemUsers();
		if(loginId!=null){
			// 提取已FOLLOW
			List<Long> follows = this.followDAO.findAllFollowByUserId(loginId);
			systemUserIds.removeAll(follows);
			systemUserIds.remove(loginId);
		}
		List<Long> userIds = new ArrayList<Long>(userSize);
		if (systemUserIds.size() > userSize) {
			for (int i = 0; i < userSize; i++) {
				userIds.add(systemUserIds.get(i));
			}
			return userDAO.findUsers(userIds);
		}else{
			if(systemUserIds.size()>0){
				return userDAO.findUsers(systemUserIds);
			}
			return new ArrayList<UserDO>();
		}
	}
	@Override
	public UserDO getUserInfo(Long loginUserId,Long otherUserId) {
		UserDO friend =this.userDAO.getUserById(otherUserId);
		FollowDO isFollow = followDAO.getFollower(loginUserId,otherUserId);
		// 判断与当前登录用户关系
		friend.setFollow(isFollow == null ? false : true);
		return friend;
	}
	// spring 注入
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void setFollowDAO(FollowDAO followDAO) {
		this.followDAO = followDAO;
	}
	
}
